package com.stylefeng.guns.modular.system.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.Tools.CodeMsg;
import com.stylefeng.guns.core.common.Tools.Result;
import com.stylefeng.guns.core.common.annotion.BussinessLog;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.core.validater.Update;
import com.stylefeng.guns.modular.system.model.OrgcategoryRole;
import com.stylefeng.guns.modular.system.service.IOrgcategoryRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 机构分类角色模板控制器
 *
 * @author fengshuonan
 * @Date 2018-09-12 17:34:43
 * sasa
 */
@Controller
@RequestMapping(value={"/gunsApi/auth/orgcategoryRole"})
@Api(value="机构分类角色模板相关api",basePath = "/gunsApi",tags={"机构分类角色模板操作接口"})
public class OrgcategoryRoleController extends BaseController {

    @Autowired
    private IOrgcategoryRoleService orgcategoryRoleService;

    /**************************************sasazaixian@2018-10-04 start*********************************/
    /**
     * 分页获取机构分类角色列表
     * @param socId
     * @param pageNum
     * @param pageSize
     * @author sasazaixian@2018-10-05
     * @return
     */
    @ApiOperation(value = "分页获取机构分类角色列表", notes = "分页获取机构分类角色列表", httpMethod = "GET")
    @GetMapping(value = "/pageList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Result pageList(
            @ApiParam(name = "socId", value = "机构分类", required = true) @RequestParam(value = "socId", required = true) String socId,
            @ApiParam(name = "pageNum", value = "当前页数", required = true) @RequestParam(value = "pageNum", required = true) Integer pageNum,
            @ApiParam(name = "pageSize", value = "每页显示条数", required = true) @RequestParam(value = "pageSize", required = true) Integer pageSize
    ) {
        try {
            OrgcategoryRole orgcategoryRole=new OrgcategoryRole();
            orgcategoryRole.setSocId(socId);
            Pager pager=new Pager();
            pager.setPageNumber(pageNum);//页码从1 开始
            pager.setPageSize(pageSize);
            pager.setObj(orgcategoryRole);
            return Result.success( orgcategoryRoleService.pageListOrgcategoryRole(pager));
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
    /**
     * 获取机构分类角色列表
     * @param socId
     * @author sasazaixian@2018-10-05
     * @return
     */
    @ApiOperation(value = "获取机构分类角色列表", notes = "获取机构分类角色列表", httpMethod = "GET")
    @GetMapping(value = "/listRole", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Result listRole(  @ApiParam(name = "socId", value = "机构分类", required = true) @RequestParam(value = "socId", required = true) String socId ) {
        try {
            OrgcategoryRole orgcategoryRole=new OrgcategoryRole();
            orgcategoryRole.setSocId(socId);
            return Result.success( orgcategoryRoleService.listOrgcategoryRole(orgcategoryRole));
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
    /**
     * 获取机构分类角色明细
     * @param sorId
     * @author sasazaixian@2018-10-05
     * @return
     */
    @ApiOperation(value = "获取功能对应的操作明细", notes = "获取功能对应的操作明细", httpMethod = "GET")
    @GetMapping(value = "/getById", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Result getById(
            @ApiParam(name = "sorId", value = "功能ID", required = true) @RequestParam(value = "sorId", required = true) String  sorId
    ) {
        try {
            return Result.success( orgcategoryRoleService.findById(sorId));
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }

    /**
     * 新增数据
     *
     * @param orgcategoryRole
     * @param result
     * @author sasazaixian@2018-10-05
     * @return
     */
    @ApiOperation(value = "新增机构分类角色操作", notes = "新增机构分类角色操作", httpMethod = "POST")
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)
    @BussinessLog(value = "新增机构分类角色操作")
    @ResponseBody
    public Result add(@RequestBody @ApiParam(name = "method", value = "新增功能操作")OrgcategoryRole orgcategoryRole, BindingResult result
    ) {
        if (result.hasErrors()) {
            return Result.error(CodeMsg.SERVER_EXCEPTION,"参数异常！");
        }
        try {
            orgcategoryRoleService.insertOrgcategoryRole(orgcategoryRole,getToken());
            return Result.success();
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }


    /**
     * 更新功能操作
     * @param orgcategoryRole
     * @param result
     * @author sasazaixian@2018-10-05
     * @return
     */
    @ApiOperation(value = "更新机构分类角色", notes = "更新机构分类角色", httpMethod = "POST")
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)
    @BussinessLog(value = "更新机构分类角色")
    @ResponseBody
    public Result update(@Validated(value = Update.class) @RequestBody @ApiParam(name = "method", value = "更新功能操作")OrgcategoryRole orgcategoryRole, BindingResult result
    ) {
        if (result.hasErrors()) {
            return Result.error(CodeMsg.SERVER_EXCEPTION,"参数异常！");
        }
        try {
            orgcategoryRoleService.updateOrgcategoryRole(orgcategoryRole,getToken());
            return Result.success();
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
    /**
     * 删除功能操作
     * @param ids
     * @author sasazaixian@2018-10-05
     * @return
     */
    @ApiOperation(value = "删除机构分类角色", notes = "删除机构分类角色", httpMethod = "GET")
    @GetMapping(value = "/del", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @BussinessLog(value = "删除机构分类角色")
    @ResponseBody
    public Result del(
            @ApiParam(name = "ids", value = "主键ID字符数组", required = true) @RequestParam(value = "ids", required = true) String ids
    ) {
        try {
            orgcategoryRoleService.delOrgcategoryRole(ids.split(","));
            return Result.success();
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
    private String getToken(){
        return getHttpServletRequest().getHeader("Authorization").substring(7);
    }
    /**************************************sasazaixian@2018-10-04 end**********************************/
}
